﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicsMarket.Service.IService;
using ElectronicsMarket.Service.Models;

namespace ElectronicsMarket.Service.ServiceImpl
{
    public class UserServiceImpl : IUserService
    {
        ElectronicsMarketDBClassesDataContext eMEntity;
        IHashPasswordService hashPasswordService = new HashPasswordServiceIpml();

        public UserServiceImpl()
        {
            eMEntity = new ElectronicsMarketDBClassesDataContext();
        }

        public CUSTOMER getUserById(int userId)
        {
            CUSTOMER customer = eMEntity.CUSTOMERs.Single(c => c.CUSTOMER_ID == userId);
            return customer;
        }
        
        public CUSTOMER getUserByUsernameAndPassword(string username, string password)
        {
            CUSTOMER customer = eMEntity.CUSTOMERs.Where(c => (c.CUSTOMER_SYS_NAME == username && c.CUSTOMER_PASSWORD == hashPasswordService.MD5Hash(password))).FirstOrDefault();
            if(customer != null)
                return customer;
            else
            {
                if (IsCreated(1) == false)
                {
                    addNewUser(1, "khach", "khach", "thattuit@yahoo.com", DateTime.Now, "khach", "khach", null, null, DateTime.Now, true);
                }
                return eMEntity.CUSTOMERs.Where(c => c.CUSTOMER_ID == 1).FirstOrDefault(); 
            }
        }

        public List<CUSTOMER> getAllUser()
        {
            List<CUSTOMER> lstcustomer = new List<CUSTOMER>();
            var alluser = from us in eMEntity.CUSTOMERs select us;

            foreach (CUSTOMER cus in alluser)
            {
                lstcustomer.Add(cus);
            }
            return lstcustomer;
        }

        public int getLastUserId()
        {
            if (IsCreated(1) == false)
            {
                addNewUser(1, "khach", "khach", "thattuit@yahoo.com", DateTime.Now, "khach", "khach", null, null, DateTime.Now, true);
            }
            return eMEntity.CUSTOMERs.OrderByDescending(cs => cs.CUSTOMER_ID).Select(cs => cs).First().CUSTOMER_ID;
        }

        public bool addNewUser(int customerId, string username, string password, string email, DateTime registerDate, string firstName, string lastName, string address, string phoneNumber, DateTime birthday, bool sex)
        {
            bool result = false;
            try
            {
                CUSTOMER customer = new CUSTOMER();
                customer.CUSTOMER_ID = customerId;
                customer.CUSTOMER_SYS_NAME = username;
                customer.CUSTOMER_PASSWORD = hashPasswordService.MD5Hash(password);
                customer.CUSTOMER_EMAIL = email;
                customer.CUSTOMER_DAY_TO_REGISTER = registerDate;
                customer.CUSTOMER_FISRT_NAME = firstName;
                customer.CUSTOMER_LAST_NAME = lastName;
                customer.CUSTOMER_PHONE = phoneNumber;
                customer.CUSTOMER_ADDRESS = address;
                customer.CUSTOMER_BIRTHDAY = birthday;
                customer.CUSTOMER_SEX = sex;
                eMEntity.CUSTOMERs.InsertOnSubmit(customer);
                eMEntity.SubmitChanges();
                result = true;
            }
            catch (Exception e)                                        
            {
                e.InnerException.ToString();
            }
            return result;
        }

        public void deleteUser(CUSTOMER customer)
        {
            CUSTOMER cus = eMEntity.CUSTOMERs.Single(c => c.CUSTOMER_ID == customer.CUSTOMER_ID);
            eMEntity.CUSTOMERs.DeleteOnSubmit(cus);

            try
            {
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }
        }

        public bool IsCreated(int customerId)
        {
            if (eMEntity.CUSTOMERs.Where(c => c.CUSTOMER_ID == customerId).FirstOrDefault() == null)
                return false;
            return true;
        }

        public bool editUser(int userId, string address, string email)
        {
            try
            {
                CUSTOMER customer = eMEntity.CUSTOMERs.Where(c => c.CUSTOMER_ID == userId).FirstOrDefault();

                customer.CUSTOMER_ADDRESS = address;
                customer.CUSTOMER_EMAIL = email;

                eMEntity.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}